package project3t.android.gui;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import project3t.android.control.Controller;
import project3t.android.data.Person;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainListActivity extends ListActivity {
    /** Called when the activity is first created. */	
	public MainListActivity() {
		super();        
	}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    }
    
    @Override
    public void onStart() {
        super.onStart();        
        setListAdapter(new ArrayAdapter<Person> (this, R.layout.list_item, Controller.conroller.getPeople()));
        //setListAdapter(new PhotoAdapter(this, R.layout.list_item, Controller.conroller.getPeople()));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {        	
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {							
	            //Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setClassName(MainListActivity.this, PersonInfo.class.getName());
				Bundle extras=new Bundle();
				extras.putInt("index", position);
				intent.putExtras(extras);
				startActivity(intent);			
			}
        });
    }
    @Override
    public void onPause(){
    	super.onPause();
    }
    @Override
    public void onSaveInstanceState(Bundle outState){
    	super.onSaveInstanceState(outState);
    	//outState.putAll(map);
    }
    
    @Override
    public Object onRetainNonConfigurationInstance(){
    	return null;
    }
    
    
    private class PhotoAdapter extends ArrayAdapter<Person>{
    	private LinkedList<Person> people;
    	private Activity context;
    	public PhotoAdapter(Context context, int textViewResourceId,
    			List<Person> people) {
    		super(context, textViewResourceId, people);
    		this.people=(LinkedList<Person>) people;
    		
    	}
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent)
    	{
    		View view=convertView;
    		TextView shortInfo=(TextView) view.findViewById(R.id.personInItem);
    		shortInfo.setText(people.get(position).toString());
    		return view;
    	}
    	
    	
    }
}